package com.baizhi.service.impl;

import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.dao.AdminDAO;
import com.baizhi.dao.RoleDAO;
import com.baizhi.entity.Admin;
import com.baizhi.entity.Resource;
import com.baizhi.entity.Role;
import com.baizhi.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class RolePermissionServiceImpl implements RolePermissionService {
    @Autowired
    private AdminDAO adminDAO;
    @Autowired
    private RoleDAO roleDAO;
    @Override
    @AddCacheAnnotation
    public Set<String> selectRoleByUsername(String userName) {
        Set<String> roleNames = new HashSet<>();
        Admin admin = adminDAO.selectAllRole(userName);
        List<Role> roles = admin.getRoles();
        for (Role role : roles) {
            roleNames.add(role.getRoleName());
        }

        return roleNames;
    }

    @Override
    @AddCacheAnnotation
    public Set<String> selectPermissionUsername(String userName) {
        Admin admin = adminDAO.selectAllRole(userName);
        Set<String> permissions = new HashSet<>();
        List<Role> roles = admin.getRoles();
        
            for (Role role : roles) {
                Role roles1 = roleDAO.selectAllResource(role.getRoleName());
                List<Resource> resources = roles1.getResources();
                for (Resource resource : resources) {
                    permissions.add(resource.getResourcePermission());
                }
            }
        
        return permissions;
    }
}
